//variable initialization 
var current_page	=	1;
var loading			=	false;
var oldscroll		=	0;
$(document).ready(function(){
var catid = $("#catid").val();
	$.ajax({
		url:'get_data.php?action=loadmore&catid='+catid,
		type:'post',
		dataType: "html",
		data: 'p='+current_page+'catid='+catid,
		success:function(data){
			//var data	=	$.parseJSON(data);
			$('#messages-list').html(data);
			//$(data.html).hide().appendTo('#messages-list').fadeIn(1000);
			current_page++;
		}
	},'html');
	var howMany = 1;
	$('#load-more').click(function(){
	document.getElementById("load_more_gif").style.display="block";
			howMany += 1;
			$("#info").text(howMany);
			$.ajax({
				url:"get_data.php?action=loadmore&catid="+catid,
				type:"post",
				'data': 'p='+howMany,
				success:function(data){
					$('#messages-list').append(data);
				}
			});
			 
		});
	
	$(window).scroll(function() {
	
		if( $(window).scrollTop() >= oldscroll ){ //if we are scrolling down
			if( ($(window).scrollTop() + $(window).height() >= $(document).height()  ) && (current_page <= total_pages) ) {
				   if( ! loading ){
						loading = true;
						$('#messages-list').addClass('loading');
						$.ajax({
							'url':'get_data.php',
							'type':'post',
							'data': 'p='+current_page,
							success:function(data){
								var data	=	$.parseJSON(data);
								$(data.html).hide().appendTo('#messages-list').fadeIn(1000);
								current_page++;
								$('#messages-list').removeClass('loading');
								loading = false;
							}
						});	
				   }
			}
		}
	});
	
});